class TaskManager
	def initialize tasks
		@tasks = tasks
	end
	
	def addTask task
    @tasks << task
	end
	
	def getTaskById id
		@tasks.each { |task|
			return task if task.id.eql?id
    }
	end

	def getTask index
		return @tasks.fetch(index)
	end

	def getTasks
		return @tasks
	end

	def updateTask task

	end

	def insertTask index,task
		return @tasks.insert(index,task)
	end

	def deleteTaskById id
    @tasks.delete(getTaskById(id))
	end

	def deleteTaskAndChildrenById id
		if hasChildren id
			getChildren(id,nil,getIdHash).each do |cid|
			  deleteTaskById cid
			end
		end
	  deleteTaskById id
	end
	
	def getIdHash
		hash = Hash.new
		@tasks.each do|task|
		  hash[task.parentId] = task.id if task.parentId != nil && !"".eql?(task.parentId)
    end
    return hash
	end
	
	def getChildren id,children,hash
		children = Array.new if children == nil
		if hash.has_key?id
			children << hash[id];
			getChildren hash[id],children,hash if hasChildren hash[id]
		end
		return children
	end
	
	def hasChildren id
		@tasks.each do|task|
		  return true if task.parentId.eql?id
	  end
	  return false
	end
	
	def deleteTask index
		return @tasks.delete_at(index)
	end
	
	def replaceTasks tasks
	  @tasks.replace tasks
	end

end